//
//  CustomerNavigationBar.m
//  HomeOfAcne
//
//  Created by 何文虎 on 2017/3/24.
//  Copyright © 2017年 hewenhu. All rights reserved.
//

#import "SSNavigationBar.h"

@implementation SSNavigationBar

@synthesize rightBtn = _rightBtn;

- (instancetype)initUsernaviBarWithFrame:(CGRect)frame{
    if (self==[super initWithFrame:frame]) {
        [self setup];
        [self addUserinformationView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self==[super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    self.backgroundColor = [UIColor whiteColor];

    self.titleLable.frame = CGRectMake(50, StatusBarHeight, self.frame.size.width - 100, self.frame.size.height-StatusBarHeight);
    self.titleLable.textColor = UIColorFromHex(0x3C3C3C);
    self.titleLable.font = [UIFont systemFontOfSize:16];
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLable];
    
    
    [self addSubview:self.leftBtn];
    self.leftBtn.frame = CGRectMake(0, StatusBarHeight, 50, NavBarHeight-StatusBarHeight);
    [self.leftBtn setImage:ImageNamed(@"nav_closeBlack.png") forState:UIControlStateNormal];
//    [self.leftBtn setImageEdgeInsets:UIEdgeInsetsMake(12, 19, 12, 19)];
    [self.leftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.rightBtn];
    self.rightBtn.frame = CGRectMake(ScreenWidth - 50, StatusBarHeight, 50, NavBarHeight-StatusBarHeight);
//    [self.rightBtn setImage:ImageNamed(@"nav_back.png") forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.hidden = YES;
    
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(0, NavBarHeight-1, ScreenWidth, 1)];
    _lineView.backgroundColor = LINEBACK_COLOR;
    [self addSubview:_lineView];
    
}

- (void)addUserinformationView{
    
    self.titleLable.hidden = YES;
    
    [self addSubview:self.usernameLb];
    [self addSubview:self.usernameLb];
    [self.usernameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(StatusBarHeight);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.height.mas_equalTo(44);
    }];
    
    [self addSubview:self.userheaderImgview];
    [self.userheaderImgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.usernameLb.mas_left).offset(-7);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.mas_equalTo(self.usernameLb.mas_centerY);
    }];
    
    _userheaderImgview.layer.cornerRadius = 15;
    _userheaderImgview.layer.masksToBounds = YES;
    
    
    
}

- (void)displayUserInformationWithName:(NSString*)name headerimgUrl:(NSString*)headerimgUrl{
    
    _usernameLb.text = BLANKSTRING(name);
    
    CGFloat width = [name boundingRectWithSize:CGSizeMake(200, 20) font:PFSC_SemiboldFont(15) lineSpacing:5].width;
    
    [_usernameLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
    
    [_userheaderImgview sd_setImageWithURL:[NSURL URLWithString:BLANKSTRING(headerimgUrl)] placeholderImage:nil];
    
}

-(void)setTitle:(NSString *)title{
    _title = title;
    _titleLable.text = title;
}

- (void)showNaviBarLine{
    
    _lineView.hidden = NO;
}

- (void)hiddenNaviBarLine{
    _lineView.hidden = YES;
}

- (void)showShareBarItem{
    self.rightBtn.hidden = NO;
}

- (void)hiddenShareBarItem{
    self.rightBtn.hidden = YES;
}

- (void)showUserinformation{
    self.userheaderImgview.hidden = NO;
    self.usernameLb.hidden = NO;
}

- (void)hiddenUserinformation{
    self.userheaderImgview.hidden = YES;
    self.usernameLb.hidden = YES;
}

- (void)configMynavibarStyle:(SSNavigationBarStyle)navibarstyle{
    if (navibarstyle == SSNavigationBarWhiteStyle) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.titleLable.textColor = UIColorFromHex(0xFFFFFF);
            [self.leftBtn setImage:ImageNamed(@"nav_closeWhite.png") forState:UIControlStateNormal];
            [self.rightBtn setImage:ImageNamed(@"nav_shareWhite.png") forState:UIControlStateNormal];
        });
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            self.titleLable.textColor = UIColorFromHex(0x3C3C3C);
            [self.leftBtn setImage:ImageNamed(@"nav_closeBlack.png") forState:UIControlStateNormal];
            [self.rightBtn setImage:ImageNamed(@"nav_shareBlack.png") forState:UIControlStateNormal];
        });
    }
}

- (void)backAction:(UIButton*)sender{
    if (self.backBlock) {
        self.backBlock();
    }
  
}

- (void)rightAction:(id)sender
{
    Block_AbleE(self.RightBlock);
}

- (UILabel*)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]init];
        _titleLable.textColor = UIColorFromHex(0x3C3C3C);
        _titleLable.font = PFSC_SemiboldFont(15);
    }
    return _titleLable;
}

- (UIImageView*)userheaderImgview{
    if (!_userheaderImgview ) {
        _userheaderImgview = [[UIImageView alloc]init];
    }
    return _userheaderImgview;
}

- (UILabel*)usernameLb{
    if (!_usernameLb) {
        _usernameLb = [[UILabel alloc]init];
        _usernameLb.textColor = UIColorFromHex(0x3C3C3C);
        _usernameLb.font = PFSC_SemiboldFont(15);
        _usernameLb.textAlignment = NSTextAlignmentCenter;
    }
    return _usernameLb;
}

- (UIButton*)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    }
         return _leftBtn;
}

- (UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    }
    return _rightBtn;
}

- (void)setRightBtn:(UIButton *)rightBtn
{
    _rightBtn = rightBtn;
//    self.rightBtn.frame = CGRectMake(ScreenWidth - 50, StatusBarHeight, 50, NavBarHeight-StatusBarHeight);
    [self.rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightBtn];
}

@end
